<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller;
use think\Controller as ThinkController;

class Wapp extends ThinkController {

    public function index()
    {
        $m = $_GET['m'];//SmallApp
        $c = $_GET['c'];//weixin
        $a = $_GET['a'];//login,getQuestion ,questionID,chapter,special,collection,submitAnswer,mnksHandPaper
        $trd_session_key = $_GET['trd_session_key'];
        $code = $_GET['code'];
        $subject = $_GET['subject'];
        $type = $_GET['type'];
        $city = $_GET['city'];
        $chapterFirstID = $_GET['chapterFirstId'];
        $chapterID = $_GET['chapterID'];
        $questionID = $_GET['questionID'];
        $mnksRecordID = $_GET['mnksRecordID'];
        $userType = $_GET['userType'];
        $colletion = $_GET['colletion'];
        $answer = $_GET['answer'];
        $choose = $_GET['choose'];
        $record = $_GET['record'];
        $useTime = $_GET['useTime'];
        if($a == 'login'||$a == 'homepage')
        //登录
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=login&openid=undefined&code=081MXaZY0Qgzu22gtg0Z0Fc9ZY0MXaZ1
        {
            $ret=array(
                'status'=>0,
                'msg'=>'失败'
                );
            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'questionID')
        //查询分类题目号
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=questionID&openid=undefined&subject=kemu1&type=sxlx&city=%E6%9D%AD%E5%B7%9E&chapterID=null&mnksRecordID=null
        {
            $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
            {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db( $conn, 'haoyayitiku' );
            if($type == 'wdct') $type = 'mnks';
            if($mnksRecordID != 'null') $mnksRecordID = 'null';
            if($type == 'sjlx')
            {
                $sql = "select id from haoyayitiku_tm_info where subject = '$subject' ";
                //echo $sql;
                $result = mysqli_query($conn, $sql);
                $queryNum = mysqli_num_rows($result);
                if ( $queryNum> 0) {
                // 输出数据
                    $sjNum = rand(0,$queryNum-10);
                    $sql = "select id from haoyayitiku_tm_info where subject = '$subject' limit $sjNum,10";
                    //echo $sql;
                    $result = mysqli_query($conn, $sql);
                    $data = array();
                    //var_dump($row);
                    //var_dump($questionArr);
                    while ($row = mysqli_fetch_assoc($result))
                    {
                        //var_dump($row);
                        $data[] = array(
                            "question_id"=> "{$row['id']}",
                            "answer"=> 0,
                            "choose"=>0,
                            "collection"=> 0
                            );
                    }
                    //var_dump($data);
                    $ret=array(
                        'status'=>1,
                        'msg'=>'成功',
                        'count'=>  mysqli_num_rows($result),
                        'data'=>$data
                        );
                } else {
                    $ret=array(
                        'status'=>0,
                        'msg'=>'失败'
                        );
                }
            }elseif($type == 'sxlx'||$type == 'mnks')
            {
                $sql1 = '';
                if($type == 'mnks') $sql1 = " limit 1,1";  //模拟练习只选取前1条题目
                $sql = "select id from haoyayitiku_tm_info where subject = '$subject' ".$sql1;
                //echo $sql;
                $result = mysqli_query($conn, $sql);
                $queryNum = mysqli_num_rows($result);
                if ($queryNum>= 500){   //顺序练习题目数量超过1000，只取前1000道
                    for ($i=1;$i<=500;$i++)
                    {
                        $row = mysqli_fetch_assoc($result);
                        //var_dump($row);
                        $data[] = array(
                            "question_id"=> "{$row['id']}",
                            "answer"=> 0,
                            "choose"=>0,
                            "collection"=> 0
                            );
                    }
                    //var_dump($data);
                    $ret=array(
                        'status'=>1,
                        'msg'=>'成功',
                        'count'=> 500,
                        'data'=>$data
                        );
                }elseif ( $queryNum> 0 && $queryNum<500) {
                    while ($row = mysqli_fetch_assoc($result))
                    {
                        //var_dump($row);
                        $data[] = array(
                            "question_id"=> "{$row['id']}",
                            "answer"=> 0,
                            "choose"=>0,
                            "collection"=> 0
                            );
                    }
                    //var_dump($data);
                    $ret=array(
                        'status'=>1,
                        'msg'=>'成功',
                        'count'=> $queryNum,
                        'data'=>$data
                        );
                } else {
                    $ret=array(
                        'status'=>0,
                        'msg'=>'失败'
                        );
                }
            }else{
                $sql1 = '';$sql2 = '';$sql3 = '';
                if(!empty($type)&&($type!='null')) $sql1 = " and type='$type'";
                if(!empty($chapterID)&&($chapterID!='null')) $sql2 = " and chapterID='$chapterID'";
                if(!empty($mnksRecordID)&&($mnksRecordID!='null')) $sql3 = " and mnksRecordID='$mnksRecordID'";
                $sql = "select questionList from haoyayitiku_tm_type where subject = '$subject' ".$sql1.$sql2.$sql3;
                //echo $sql;
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                // 输出数据
                    //while($row = mysqli_fetch_assoc($result)) {
                    //    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                    //        }
                    $row = mysqli_fetch_assoc($result);
                    $questionArr = json_decode($row['questionList']);
                    $data = array();
                    //var_dump($row);
                    //var_dump($questionArr);
                    if(isset($trd_session_key)&&$trd_session_key!='{}')
                    {
                        $user_id = explode('.@.', $trd_session_key)[0];
                        $sql_sel = "select * from haoyayitiku_user_record where user_id = $user_id and subject = '$subject' and chapterID = $chapterID;";
                        $result = mysqli_query($conn, $sql_sel);
                        $queryNum = mysqli_num_rows($result);
                        if($result>0){
                            $row = mysqli_fetch_assoc($result);
                            $answerList = json_decode($row['answerList'],true);
                            foreach ($questionArr as $value)
                            {
                                $answerMy = 0;
                                if(isset($answerList["$value"])) $answerMy = $answerList["$value"];
                                $data[] = array(
                                    "question_id"=> "$value",
                                    "answer"=> $answerMy,
                                    "choose"=>0,
                                    "collection"=> 0
                                    );
                            }
                        }else{
                            $ret=array(
                                'status'=>0,
                                'msg'=>'失败'
                            );
                        }
                    }else{
                        foreach ($questionArr as $value)
                        {
                            $data[] = array(
                                "question_id"=> "$value",
                                "answer"=> 0,
                                "choose"=>0,
                                "collection"=> 0
                                );
                        }
                    }
                    //var_dump($data);
                    $ret=array(
                        'status'=>1,
                        'msg'=>'成功',
                        'count'=>  count($questionArr),
                        'data'=>$data
                        );
                } else {
                    $ret=array(
                        'status'=>0,
                        'msg'=>'失败'
                        );
                }
            }
            mysqli_close($conn);

            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'getQuestion')
        //查询问题详情
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=chapter&openid=undefined&subject=kemu1&type=zjlx
        { 
            $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
            {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db( $conn, 'haoyayitiku' );
            //var_dump($questionID);
            if(!empty($subject)&&!empty($questionID))
            {
                $questionID = json_decode($questionID);
                $data = array();
                foreach ($questionID as $value) {
                    $sql = "select * from haoyayitiku_tm_info where subject = '$subject' and id = '$value' ";
                    $result = mysqli_query($conn, $sql);
                    //echo $sql;
                    if (mysqli_num_rows($result) > 0) {
                        $row = mysqli_fetch_assoc($result);
                        //var_dump($row);
                        $wrong_rate = $row['false_count']/($row['true_count']+$row['true_count']);
                        $data[] = array(
                            "id_"=> "{$row['id']}",
                            "question_id"=> "{$row['id']}",
                            "media_type"=> "{$row['media_type']}",
                            "chapter_id"=> "{$row['chapter_id']}",
                            "label_"=> "4.1.1.106",
                            "question_"=> "{$row['question']}",
                            "media_content"=> "{$row['media_content']}",
                            "media_width"=> "{$row['media_width']}",
                            "media_height"=> "{$row['media_height']}",
                            "answer_"=> "{$row['answer']}",
                            "option_a"=> "{$row['option_a']}",
                            "option_b"=> "{$row['option_b']}",
                            "option_c"=> "{$row['option_c']}",
                            "option_d"=> "{$row['option_d']}",
                            "option_e"=> "{$row['option_e']}",
                            "option_f"=> "{$row['option_f']}",
                            "option_g"=> "{$row['option_g']}",
                            "option_h"=> "{$row['option_h']}",
                            "explain_"=> "{$row['explain_quest']}",
                            "difficulty_"=> "{$row['difficulty_']}",
                            "false_count"=> "{$row['false_count']}",
                            "true_count"=> "{$row['true_count']}",
                            "wrong_rate"=> "$wrong_rate",
                            "option_type"=> "{$row['option_type']}",
                            "collection"=> 0,
                            "choose"=> 0,
                            "answer"=> 0
                        );
                    }
                }
                $ret=array(
                    'status'=>1,
                    'msg'=>'成功',
                    'data'=>$data
                    );
            }else {
                $ret=array(
                    'status'=>0,
                    'msg'=>'失败'
                    );
            }
            mysqli_close($conn);

            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'chapterFirst')
        //查询一级分类章节
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {
        $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
            {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db( $conn, 'haoyayitiku' );
            $sql1 = '';
            if(!empty($type)&&($type!='null')) $sql1 = " and type='$type'";
            $sql = "select  chapterFirstId ,chapterFirstName,count(*) count from haoyayitiku_tm_type where chapterFirstId <>'' AND subject = '$subject' ".$sql1.'GROUP BY chapterFirstId';
            //echo $sql;
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
            // 输出数据
                //while($row = mysqli_fetch_assoc($result)) {
                //    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                //        }
                $data = array();
                while($row = mysqli_fetch_assoc($result))
                {
                    //var_dump($row);
                    $data[] = array(
                        "id"=> "{$row['chapterFirstId']}",
                        "title"=> "{$row['chapterFirstName']}",
                        "count"=> "{$row['count']}"
                        );
                }
                //var_dump($data);
                $ret=array(
                    'status'=>1,
                    'msg'=>'成功',
                    'data'=>$data
                    );
            } else {
                $ret=array(
                    'status'=>0,
                    'msg'=>'失败'
                    );
            }
            mysqli_close($conn);

            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'chapter')
        //查询二级分类章节
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {
            $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
            {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db( $conn, 'haoyayitiku' );
            $sql1 = '';
            if(!empty($type)&&($type!='null')) $sql1 = " and type='$type'";
            $sql2 = '';
            if(!empty($chapterFirstID)) $sql2 = " and chapterFirstId='$chapterFirstID'";
            $sql = "select * from haoyayitiku_tm_type where subject = '$subject' ".$sql1.$sql2;
            //echo $sql;
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
            // 输出数据
                //while($row = mysqli_fetch_assoc($result)) {
                //    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                //        }
                $data = array();
                while($row = mysqli_fetch_assoc($result))
                {
                    //var_dump($row);
                    $data[] = array(
                        "level"=> "{$row['level']}",
                        "id"=> "{$row['chapterID']}",
                        "title"=> "{$row['Name']}",
                        "count"=> "{$row['questionNum']}"
                        );
                }
                //var_dump($data);
                $ret=array(
                    'status'=>1,
                    'msg'=>'成功',
                    'data'=>$data
                    );
            } else {
                $ret=array(
                    'status'=>0,
                    'msg'=>'失败'
                    );
            }
            mysqli_close($conn);

            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'special')
        //查询专项
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {
            $dbhost = 'localhost';  // mysql服务器主机地址
            $dbuser = 'root';            // mysql用户名
            $dbpass = 'hahacscs';          // mysql用户名密码
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
            if(! $conn )
            {
                die('连接失败: ' . mysqli_error($conn));
            }
            mysqli_select_db( $conn, 'haoyayitiku' );
            $sql1 = '';
            if(!empty($type)&&($type=='zxlx')) $sql1 = " and special>0";
            $sql = "select * from haoyayitiku_tm_type where subject = '$subject' ".$sql1;
            //echo $sql;
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
            // 输出数据
                //while($row = mysqli_fetch_assoc($result)) {
                //    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
                //        }
                $data = array();$data1 = array();$data2 = array();$data3 = array();
                while($row = mysqli_fetch_assoc($result))
                {
                    //var_dump($row);
                    if($row['special']==1){
                        $data1[] = array(
                            "text"=> "{$row['Name']}",
                            "type"=> "{$row['type']}"
                            );
                    }elseif($row['special']==2){
                        $exertip = explode(',',$row['Name'])[0];
                        $text = explode(',',$row['Name'])[1];
                        $data2[] = array(
                            "exerTip"=> "$exertip",
                            "text"=> "$text",
                            "type"=> "{$row['type']}",
                            );
                    }elseif($row['special']==3){
                        $data3[] = array(
                            "text"=> "{$row['Name']}",
                            "examNum"=> "{$row['questionNum']}",
                            "type"=> "{$row['type']}",
                            "chapterID"=> "{$row['chapterID']}",
                            );
                    }else{
                        $ret=array(
                        'status'=>0,
                        'msg'=>'失败'
                    );
                    }
                }
                $data = [$data1,$data2,$data3];
                //var_dump($data);
                $ret=array(
                    'status'=>1,
                    'msg'=>'成功',
                    'data'=>$data
                    );
            } else {
                $ret=array(
                    'status'=>0,
                    'msg'=>'失败'
                    );
            }
            mysqli_close($conn);

            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'collection')
        //是否收藏
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {
            $ret=array(
                'status'=>1,
                'msg'=>'成功',
                'data'=>array(
                    array(
                        "id"=> "1",
                        "title"=> "title1",
                        "count"=> "520"
                        ),
                    array(
                        "id"=> "2",
                        "title"=> "title2",
                        "count"=> "520"
                        ),
                    array(
                        "id"=> "3",
                        "title"=> "title3",
                        "count"=> "520"
                        )
                    )
                );
            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'submitAnswer')
        //提交答案
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {    
            if($trd_session_key!='{}'&&!empty($chapterID)&&($chapterID!='null')){
                $user_id = explode('.@.', $trd_session_key)[0];
                $dbhost = 'localhost';  // mysql服务器主机地址
                $dbuser = 'root';            // mysql用户名
                $dbpass = 'hahacscs';          // mysql用户名密码
                $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
                if(! $conn )
                {
                    die('连接失败: ' . mysqli_error($conn));
                }
                mysqli_select_db( $conn, 'haoyayitiku' );
                $sql_sel = "select * from haoyayitiku_user_record where user_id = $user_id and subject = '$subject' and chapterID = $chapterID;";
                $result = mysqli_query($conn, $sql_sel);
                $queryNum = mysqli_num_rows($result);
                $answerList = array();
                if($result>0&&$queryNum==0){
                    $answerList["$questionID"] = $answer;
                    $answerList = json_encode($answerList);
                    $sql_insert = "insert into haoyayitiku_user_record (user_id,subject,chapterID,answerList) values ($user_id,'$subject',$chapterID,'$answerList')";
                    $retval  = mysqli_query($conn, $sql_insert);
                    if(! $retval )
                    {
                        $ret=array(
                            'status'=>0,
                            'msg'=>'数据插入失败'
                            );
                    }
                    $ret=array(
                        'status'=>1,
                        'msg'=>'数据插入成功'
                    );
                }elseif($result>0&&$queryNum==1){
                    $row = mysqli_fetch_assoc($result);
                    $answerList = json_decode($row['answerList'],true);
                    $answerList["$questionID"] = $answer;
                    $answerList = json_encode($answerList);
                    $sql_update = "update haoyayitiku_user_record set answerList = '$answerList' where user_id = $user_id and subject = '$subject' and chapterID = $chapterID;";
                    $retval  = mysqli_query($conn, $sql_update);
                    if(! $retval )
                    {
                        $ret=array(
                            'status'=>0,
                            'msg'=>'数据更新失败'
                            );
                    }
                    $ret=array(
                        'status'=>1,
                        'msg'=>'数据更新成功'
                    );
                }else{
                    $ret=array(
                        'status'=>0,
                        'msg'=>'失败'
                    );
                }
            }
            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }elseif($a == 'mnksHandPaper')
        //提交试卷
        //https://www.1217.com//weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion&openid=undefined&questionID=%5B896900%2C897000%2C897100%2C897200%2C1102000%2C1102100%5D&subject=kemu1
        {
            $record = json_decode($record);
            $all = 0; $right = 0;
            //var_dump($record);
            foreach ($record as $value) {
                //var_dump($value);
                $all++;
                $tmp = json_decode(json_encode($value), true);//对象转数组方法
                //var_dump($tmp);
                if($tmp['answer']==1) $right++;
            }
            $score =  ceil(($right/$all)*100);
            $ret=array(
                'status'=>1,
                'msg'=>'成功',
                'data'=>array(
                    "mid"=> "MP". time(),
                    "score"=> $score 
                    )
                );
            //var_dump($ret);
            $ret = json_encode($ret);
            //var_dump($ret);
            echo $ret;
            return ;
        }
    }
}
    
    
